﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Architecture.ComponentLogic.ElementModel;

namespace AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.Service
{
    public class ElementBuilderBusinessLogic : ElementBuilder
    {
        public override void BuildStructure(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            BuildBusinessLogicElement(viewModel, templateRepository, itemEntity);
        }
        public void BuildBusinessLogicElement(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = templateRepository.GetPackageByName("Servico").GetPackageByName("BusinessLogic");
            AddIn.EA.Framework.Element templateBusinessLogicElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the BusinessEntity package
            ComponentLogic.ElementModel.Package servicoPackage = GetViewModelPackage("Servico", viewModel, true);
            ComponentLogic.ElementModel.Package businessLogicPackage = GetViewModelPackage("BusinessLogic", servicoPackage, true);

            String elementName = templateBusinessLogicElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            businessLogicPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Build BusinessEntity attributes.
            Element businessLogicElement = BuildEntityElementFromTemplate(itemEntity, templateBusinessLogicElement, elementName);

            //Add the methods defined for this layer.
            BindElementMethods(businessLogicElement, itemEntity, "BusinessEntity", "BusinessEntity");

            businessLogicPackage.Elements.Add(businessLogicElement);
        }

        public override void Validate()
        {
        }
    }
}
